﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using MugenMvvmToolkit.Interfaces;

namespace MugenMvvmToolkit.Models
{
    /// <summary>
    ///     Represents a container for the results of a validation request.
    /// </summary>
#if WPF
    [Serializable]
#endif
    [DataContract]
    internal sealed class ValidationResultWrapper : IValidationResult
    {
        #region Fields

        private readonly ValidationResult _validationResult;

        #endregion

        #region Constructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="ValidationResultWrapper" /> class by using a
        ///     <see cref="ValidationResultWrapper" />
        ///     object.
        /// </summary>
        /// <param name="validationResult">The validation result object.</param>
        internal ValidationResultWrapper(ValidationResult validationResult)
        {
            _validationResult = validationResult;
        }

        #endregion

        #region Properties

        /// <summary>
        ///     Gets the value which contains validation result.
        /// </summary>
        public bool IsValid
        {
            get { return _validationResult == ValidationResult.Success; }
        }

        /// <summary>
        ///     Gets the collection of member names that indicate which fields have validation errors.
        /// </summary>
        /// <returns>
        ///     The collection of member names that indicate which fields have validation errors.
        /// </returns>
        public IEnumerable<string> MemberNames
        {
            get
            {
                if (_validationResult == null)
                    return Enumerable.Empty<string>();
                return _validationResult.MemberNames;
            }
        }

        /// <summary>
        ///     Gets the error message for the validation.
        /// </summary>
        /// <returns>
        ///     The error message for the validation.
        /// </returns>
        public string ErrorMessage
        {
            get
            {
                if (_validationResult == null)
                    return null;
                return _validationResult.ErrorMessage;
            }
            set { _validationResult.ErrorMessage = value; }
        }

        #endregion

        #region Overrides of Object

        /// <summary>
        ///     Returns a string representation of the current validation result.
        /// </summary>
        /// <returns>
        ///     The current validation result.
        /// </returns>
        public override string ToString()
        {
            return ErrorMessage;
        }

        #endregion
    }
}